package com.hjm.project.recruitTeam.recruitTeam.controller;

import java.util.List;
import java.util.Map;

import com.hjm.framework.config.CustomConfig;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.hjm.framework.aspectj.lang.annotation.Log;
import com.hjm.framework.aspectj.lang.enums.BusinessType;
import com.hjm.project.recruitTeam.recruitTeam.domain.RecruitTeam;
import com.hjm.project.recruitTeam.recruitTeam.service.IRecruitTeamService;
import com.hjm.framework.web.controller.BaseController;
import com.hjm.framework.web.domain.AjaxResult;
import com.hjm.common.utils.poi.ExcelUtil;
import com.hjm.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 招募团队文案Controller
 * 
 * @author LOVE
 * @date 2020-11-26
 */
@Controller
@RequestMapping("/recruitTeam/recruitTeam")
public class RecruitTeamController extends BaseController
{
    private String prefix = "recruitTeam/recruitTeam";

    @Autowired
    private IRecruitTeamService recruitTeamService;

    @RequiresPermissions("recruitTeam:recruitTeam:view")
    @GetMapping()
    public String recruitTeam()
    {
        return prefix + "/recruitTeam";
    }

    /**
     * 查询招募团队文案列表
     */
    @RequiresPermissions("recruitTeam:recruitTeam:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RecruitTeam recruitTeam)
    {
        startPage();
        List<RecruitTeam> list = recruitTeamService.selectRecruitTeamList(recruitTeam);
        return getDataTable(list);
    }

    /**
     * 导出招募团队文案列表
     */
    @RequiresPermissions("recruitTeam:recruitTeam:export")
    @Log(title = "招募团队文案", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RecruitTeam recruitTeam)
    {
        List<RecruitTeam> list = recruitTeamService.selectRecruitTeamList(recruitTeam);
        ExcelUtil<RecruitTeam> util = new ExcelUtil<RecruitTeam>(RecruitTeam.class);
        return util.exportExcel(list, "recruitTeam");
    }

    /**
     * 新增招募团队文案
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存招募团队文案
     */
    @RequiresPermissions("recruitTeam:recruitTeam:add")
    @Log(title = "招募团队文案", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MultipartFile typePic, RecruitTeam recruitTeam)throws Exception {
        if (recruitTeam.getMsg() != null && recruitTeam.getMsg().length() > 100) {
            return error("消息内容长度不可大于100");
        }
        if (recruitTeam.getMsg() != null){
            String msg = recruitTeam.getMsg().replaceAll("undefined,", "");
            recruitTeam.setMsg(msg);
        }
        if (recruitTeam.getMsg().equals("undefined")) {
            recruitTeam.setMsg(null);
        }
        return toAjax(recruitTeamService.insertRecruitTeam(typePic,recruitTeam));
    }

    /**
     * 修改招募团队文案
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        RecruitTeam recruitTeam = recruitTeamService.selectRecruitTeamById(id);
        recruitTeam.setImgUrl(CustomConfig.getImgUrl()+recruitTeam.getImgUrl());
        mmap.put("recruitTeam", recruitTeam);
        return prefix + "/edit";
    }

    /**
     * 修改保存招募团队文案
     */
    @RequiresPermissions("recruitTeam:recruitTeam:edit")
    @Log(title = "招募团队文案", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MultipartFile typePic, RecruitTeam recruitTeam, @RequestParam Map map) throws Exception
    {
        if (recruitTeam.getMsg() != null && recruitTeam.getMsg().length() > 100) {
            return error("消息内容长度不可大于100");
        }
        return toAjax(recruitTeamService.updateRecruitTeam(typePic,recruitTeam));
    }

    /**
     * 删除招募团队文案
     */
    @RequiresPermissions("recruitTeam:recruitTeam:remove")
    @Log(title = "招募团队文案", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(recruitTeamService.deleteRecruitTeamByIds(ids));
    }
}
